<?php


namespace Packages\Authority\Http\Controllers;


use App\Http\Controllers\Controller;
use Packages\Authority\Services\AuthorityService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Tools\Common;

class AdminController extends Controller
{
    protected $authService;

    public function __construct(AuthorityService $authService)
    {
        $this->authService = $authService;
    }

    /**
     * 获取全部
     * @param Request $request
     * @return JsonResponse
     */
    public function roleAll(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'string|size:32',
            'guids' => 'array',
            'app_id' => 'string|max:32',
            'name' => 'string|max:32',
            'platform' => 'string|max:32',
            'status' => 'integer|between:1,2',
            'limit' => 'integer|between:1,1000',
        ]);

        $result = $this->authService->role->all($params);

        return $this->returnResponse($result);
    }

    /**
     * 获取分页列表
     * @param Request $request
     * @return JsonResponse
     */
    public function roleList(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'string|size:32',
            'app_id' => 'string|max:32',
            'name' => 'string|max:32',
            'platform' => 'string|max:32',
            'status' => 'integer|between:1,10',
            'current' => 'integer|between:1,100000',
            'pageSize' => 'integer|between:1,1000',
        ]);

        $result = $this->authService->role->list($params);

        return $this->returnResult($result);
    }

    /**
     * 添加角色
     * @param Request $request
     * @return JsonResponse
     */
    public function roleAdd(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'app_id' => 'string|max:32',
            'name' => 'required|string|max:32',
            'platform' => 'string|max:32',
            'status' => 'integer|between:1,10',
            'current' => 'integer|between:1,100000',
            'pageSize' => 'integer|between:1,1000',
        ]);

        $result = $this->authService->role->create($params);

        return $this->returnResult($result);
    }

    /**
     * 更新角色
     * @param Request $request
     * @return JsonResponse
     */
    public function roleUpdate(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|size:32',
            'name' => 'required|string|max:32',
            'status' => 'required|integer|between:1,10',
        ]);

        $result = $this->authService->role->update($params['guid'], $params);

        return $this->returnResult($result);
    }

    /**
     * 全部权限
     * @param Request $request
     * @return JsonResponse
     */
    public function authAll(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'string|size:32',
            'name' => 'string|max:32',
            'url' => 'string|max:32',
            'pid' => 'string|max:32',
            'type' => 'integer|between:1,10',
            'platform' => 'string|max:32',
            'app_id' => 'string|max:32',
            'tree' => 'boolean',
        ]);

        $result = $this->authService->auth->all($params);

        return $this->returnResponse($result);
    }

    /**
     * 添加权限
     * @param Request $request
     * @return JsonResponse
     */
    public function authAdd(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'name' => 'required|string|max:32',
            'url' => 'required|string|max:32',
            'type' => 'required|integer|between:1,10',
            'pid' => 'string|max:32',
            'platform' => 'string|max:32',
            'app_id' => 'string|max:32',
        ]);

        $result = $this->authService->auth->create($params);

        return $this->returnResult($result);
    }

    /**
     * 编辑权限
     * @param Request $request
     * @return JsonResponse
     */
    public function authUpdate(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|size:32',
            'name' => 'required|string|max:32',
            'url' => 'required|string|max:32',
            'type' => 'required|integer|between:1,10',
        ]);

        $result = $this->authService->auth->update($params['guid'], $params);

        return $this->returnResult($result);
    }

    /**
     * 删除权限
     * @param Request $request
     * @return JsonResponse
     */
    public function authDelete(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'guid' => 'required|string|size:32',
        ]);

        $result = $this->authService->auth->delete($params['guid']);

        return $this->returnResult($result);
    }

    /**
     * 指定角色全部权限
     * @param Request $request
     * @return JsonResponse
     */
    public function roleAuthAllId(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'role_id' => 'required|string|size:32',
        ]);

        $result = $this->authService->roleAuth->roleAuthAllId($params['role_id']);

        return $this->returnResult($result);
    }

    /**
     * 更新角色权限
     * @param Request $request
     * @return JsonResponse
     */
    public function editRoleAuth(Request $request)
    {
        // 定义业务参数验证规则 验证参数
        $params = $this->validateParams($request, [
            'role_id' => 'required|string|size:32',
            'auth_id' => 'required|string|size:32',
        ]);

        switch ($params['type']) {
            case 1: // 添加
                $result = $this->authService->roleAuth->create($params);
                break;
            case 2: // 删除
                $result = $this->authService->roleAuth->remove($params);
                break;
            default:
                $result = $this->returnResult('参数错误', 400);
                break;
        }

        return $this->returnResult($result);
    }
}
